package com.ntschy.web.controller.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ntschy.common.config.ProjectConfig;
import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.entity.enums.SysBusinessType;
import com.ntschy.common.utils.file.FileUtil;
import com.ntschy.framework.config.ServerConfig;
import com.ntschy.sys.service.ISysFileUpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author ezreal_geng
 */
@RestController
public class CommonController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISysFileUpService fileUpService;

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    public AjaxResult uploadFile(MultipartFile file, Integer businessType) throws Exception {
        SysBusinessType type = formatBusinessType(businessType);
        if(ObjectUtil.isNull(type)) {
            return AjaxResult.error(HttpStatus.FILE, "暂不支持改业务类型文件上传");
        }
        return fileUpService.addFileUp(file, type);
    }


    @GetMapping("/common/down")
    public ResponseEntity<Resource> downloadFile(String fileName, HttpServletRequest request) {

        String path = StrUtil.subAfter(fileName, ProjectConfig.getUploadPrefix(), true);

        Resource resource = FileUtil.loadFileAsResource(path);
        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        }
        catch (IOException e) {
            log.error("无法获取文件类型", e);
        }
        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                .body(resource);
    }


    public static SysBusinessType formatBusinessType(Integer param) {
        if(param == 0) {
            return SysBusinessType.YJQZ;
        } else if(param == 1) {
            return SysBusinessType.ZCK;
        } else if(param == 2) {
            return SysBusinessType.GXCS;
        } else if(param == 3) {
            return SysBusinessType.WJDC;
        } else if(param == 4) {
            return SysBusinessType.ZXSB;
        } else if(param == 5) {
            return SysBusinessType.ZGTS;
        } else if(param == 6) {
            return SysBusinessType.QYFC;
        } else if(param == 7) {
            return SysBusinessType.QYDSJFX;
        } else {
            return null;
        }
    }
}
